package db

import (
	"fmt"
	"strconv"

	"github.com/go-redis/redis"

	untils "yundata-service/util"
)

func InitRedis() (*redis.Client, error) {

	var cfg map[string]string
	readErr := untils.ParseYamlFile("redis.yaml", &cfg)
	if readErr != nil {
		return nil, readErr
	}

	addr := fmt.Sprintf("%s:%s", cfg["host"], cfg["port"])
	db, _ := strconv.Atoi(cfg["db"])

	rdb := redis.NewClient(&redis.Options{
		Addr:     addr,
		Password: cfg["password"],
		DB:       db,
	})

	_, pingErr := rdb.Ping().Result()
	if pingErr != nil {
		return nil, pingErr
	} else {
		fmt.Println("连接Redis成功")
	}

	return rdb, nil
}
